package com.lazyframework.generator.core;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lazyframework.commons.util.Maps;
import com.lazyframework.generator.model.Column;
import com.lazyframework.generator.model.Table;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * 基于MyBatisPlus的Provider
 *
 * Create by lazy in 2020/07/26
 */
@Slf4j
public class MpGenerateProvider extends AbstractGenerateProvider {

    @Override
    public void doInternalGenerate(Table table, String projectName, String basePackage, String module,
                                   String domainObjName) {
        String comment = table.getComment();
        List<Column> columns = table.getColumns();
        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成Entity对象 ~~~~~~~~~~~~~~~~~~~~~~~~ //
        Map<String, Object> entityParams = Maps.<String, Object>builder()
                .put("package", basePackage + ".entity." + module)
                .put("comment", comment + "实体对象")
                .put("tableName", table.getName())
                .put("clsName", domainObjName)
                .put("columns", columns)
                .build();
        printJava(projectName, "entity.ftl", entityParams);

        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成Mapper接口 ~~~~~~~~~~~~~~~~~~~~~~~~ //
        String mapperClsName = domainObjName + "Mapper";
        Map<String, Object> mapperParams = Maps.<String, Object>builder()
                .put("basePackage", basePackage)
                .put("package", basePackage + ".mapper." + module)
                .put("entity",  domainObjName)
                .put("module", module)
                .put("comment", comment + "Mapper接口")
                .put("clsName", mapperClsName)
                .build();
        printJava(projectName, "mapper.ftl", mapperParams);

        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成Mapper.xml文件 ~~~~~~~~~~~~~~~~~~~~~~~~ //
        Map<String, Object> params = Maps
                .newHashMap("namespace", basePackage + ".mapper." + module + "." + mapperClsName);
        String outPath = getRootPath() + "/" + projectName + PROJECT_RESOURCES_DIC + "mybatis/mapper/" + module;
        print("mapper_xml.ftl", params, outPath, mapperClsName + ".xml");

        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成Service接口 ~~~~~~~~~~~~~~~~~~~~~~~~ //
        Map<String, Object> serviceParams = Maps.<String, Object>builder()
                .put("basePackage", basePackage)
                .put("domainObj", domainObjName)
                .put("module", module)
                .put("comment", comment + "Service")
                // 为了生成文件时获取文件名和输出路径
                .put("package", basePackage + ".service." + module)
                .put("clsName", domainObjName + "Service")
                .build();
        printJava(projectName, "service.ftl", serviceParams);

        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成ServiceImpl ~~~~~~~~~~~~~~~~~~~~~~~~ //
        Map<String, Object> serviceImplParams = Maps.<String, Object>builder()
                .put("basePackage", basePackage)
                .put("domainObj", domainObjName)
                .put("module", module)
                .put("comment", comment)
                // 为了生成文件时获取文件名和输出路径
                .put("package", basePackage + ".service." + module + ".impl")
                .put("clsName", domainObjName + "ServiceImpl")
                .build();
        printJava(projectName, "service_impl.ftl", serviceImplParams);

        //~~~~~~~~~~~~~~~~~~~~~~~~ 生成Controller ~~~~~~~~~~~~~~~~~~~~~~~~ //
        Map<String, Object> controllerParams = Maps.<String, Object>builder()
                .put("basePackage", basePackage)
                .put("domainObj", domainObjName)
                .put("module", module)
                .put("comment", comment)
                // 为了生成文件时获取文件名和输出路径
                .put("package", basePackage + ".web." + module + ".controller")
                .put("clsName", domainObjName + "Controller")
                .put("serviceName", domainObjName.substring(0, 1).toLowerCase() + domainObjName.substring(1) + "Service")
                .put("requestMapping", StringUtils.camelToHyphen(domainObjName))
                .build();
        printJava(projectName, "controller.ftl", controllerParams);
    }

    @Override
    public boolean supports(String mp) {
        return PersistentFrameworks.MYBATIS_PLUS.name().equals(mp);
    }

}
